package cn.qayy.controller;

import cn.qayy.dto.signInDTO;
import cn.qayy.service.ISignInService;
import cn.qayy.utils.ExcelUtils;
import cn.qayy.utils.JsonResult;
import cn.qayy.utils.ResonseUtils;
import cn.qayy.utils.StringUtil;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/signIn/")
public class SignInController {
    @Resource
    private ISignInService signInService;

    private final static String[] signInTitle = new String[]{"姓名", "签到日期", "时间点","星期",
            "省份", "地市", "区县", "勤劳分", "客户","积分", "级别", "签到类型"};

    //根据开始时间和结束时间获取业务员签到记录
    @ResponseBody
    @RequestMapping(value="showSignInList")
    public String showSignIn(@RequestParam Map<String, Object> condiMap) {
    	//对map进行过滤，除去空格
    	condiMap = StringUtil.trim4Map(condiMap);
    			
        try {
        	System.out.println("showSignIn.......");
            if (StringUtils.isEmpty((String)condiMap.get("startTime"))){
                condiMap.remove("startTime");
            }
            if (StringUtils.isEmpty((String)condiMap.get("endTime"))){
                condiMap.remove("endTime");
            }
            for (Map.Entry<String, Object> entry : condiMap.entrySet()) {
                String key = entry.getKey().toString();
                String value = entry.getValue().toString();
                System.out.println("key=" + key + " value=" + value);
            }
            PageInfo<Map<String, Object>> pageInfo  = signInService.selectSignBy(condiMap);
            String pagelist =JSONObject.toJSONString(pageInfo);
            return  pagelist;
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
        return null;
    }

    //下载经理签到情况
    @RequestMapping("/downLoadSignIn")
    public JsonResult downLoadSignIn(@RequestParam Map<String, Object> condiMap,HttpServletRequest request, HttpServletResponse response) {
        try {
        	System.out.println("downLoadSignIn.......");
            JSONObject json = new JSONObject();
            //准备数据
            List<signInDTO> signIns = new ArrayList<signInDTO>(71000);
            signIns.addAll(signInService.selectSignInForDown(condiMap));
            for (signInDTO sign : signIns){
                System.out.println(sign);
            }
            //把数据写入到Excel并返回文件路径和文件
            String filePath = ExcelUtils.exportCVS(signIns, signInTitle);
            json.put("sucessful", 1);//关闭进度条
            json.put("filePath", "/servlet/FileDownServlet?url=" + filePath);//文件下载,请求Servlet
            ResonseUtils.writeResponseByJson(response, json);
            json = null;
            signIns.clear();
            signIns = null;
            filePath = null;
		} catch (Exception e) {
			// TODO: handle exception
		}
        return null;
    }
}